package com.test.reggieadmin.model.home;

import android.os.Handler;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.chaychan.library.BottomBarItem;
import com.test.reggieadmin.R;
import com.test.reggieadmin.base.BaseActivity;
import com.test.reggieadmin.base.BasePresenter;
import com.test.reggieadmin.databinding.ActivityMainBinding;
import com.test.reggieadmin.model.fragment.cpgl.CpglFragment;
import com.test.reggieadmin.model.fragment.ddmx.DdmxFragment;
import com.test.reggieadmin.model.fragment.employee.EmployeeFragment;
import com.test.reggieadmin.model.fragment.flgl.FlglFragment;
import com.test.reggieadmin.model.fragment.mycenter.MyCenterFragment;
import com.test.reggieadmin.model.fragment.tcgl.TcglFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 首页
 */
public class MainActivity extends BaseActivity<ActivityMainBinding, BasePresenter> {

    private List<Fragment> fragments = new ArrayList<>();
    private RotateAnimation mRotateAnimation;
    private Handler mHandler = new Handler();

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {
        viewBinding.bbl.setOnItemSelectedListener((bottomBarItem, previousPosition, currentPosition) -> {
            changeFragment(currentPosition);

            if (currentPosition == 0) {
                //如果是第一个，即首页
                if (previousPosition == currentPosition) {
                    //如果是在原来位置上点击,更换首页图标并播放旋转动画
                    if (mRotateAnimation != null && !mRotateAnimation.hasEnded()) {
                        //如果当前动画正在执行
                        return;
                    }

                    bottomBarItem.setSelectedIcon(R.drawable.tab_loading);//更换成加载图标 setResId

                    //播放旋转动画
                    if (mRotateAnimation == null) {
                        mRotateAnimation = new RotateAnimation(0, 360,
                                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                                0.5f);
                        mRotateAnimation.setDuration(800);
                        mRotateAnimation.setRepeatCount(-1);
                    }
                    ImageView bottomImageView = bottomBarItem.getImageView();
                    bottomImageView.setAnimation(mRotateAnimation);
                    bottomImageView.startAnimation(mRotateAnimation);//播放旋转动画

                    //模拟数据刷新完毕
                    mHandler.postDelayed(() -> {
                        boolean tabNotChanged = viewBinding.bbl.getCurrentItem() == currentPosition; //是否还停留在当前页签
                        bottomBarItem.setSelectedIcon(R.drawable.tab_yggl_selected);//更换成首页原来选中图标
                        cancelTabLoading(bottomBarItem);
                    }, 3000);
                    return;
                }
            }

            //如果点击了其他条目
            BottomBarItem bottomItem = viewBinding.bbl.getBottomItem(0);
            bottomItem.setSelectedIcon(R.drawable.tab_yggl_selected);//更换为原来的图标
            cancelTabLoading(bottomItem);//停止旋转动画
        });

        /*viewBinding.bbl.setUnread(0, 20);//设置第一个页签的未读数为20
        viewBinding.bbl.setUnread(1, 1001);//设置第二个页签的未读数
        viewBinding.bbl.showNotify(2);//设置第三个页签显示提示的小红点
        viewBinding.bbl.setMsg(3, "NEW");//设置第四个页签显示NEW提示文字*/
    }

    @Override
    protected void initData() {
        EmployeeFragment yggl = new EmployeeFragment();
        fragments.add(yggl);

        FlglFragment flgl = new FlglFragment();
        fragments.add(flgl);

        CpglFragment cpgl = new CpglFragment();
        fragments.add(cpgl);

        TcglFragment tcgl = new TcglFragment();
        fragments.add(tcgl);

        DdmxFragment ddmx = new DdmxFragment();
        fragments.add(ddmx);

        MyCenterFragment grzx = new MyCenterFragment();
        fragments.add(grzx);

        changeFragment(0); //默认显示第一页
    }

    private void changeFragment(int currentPosition) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.fl_content, fragments.get(currentPosition));
        transaction.commit();
    }

    /**
     * 停止首页页签的旋转动画
     */
    private void cancelTabLoading(BottomBarItem bottomItem) {
        Animation animation = bottomItem.getImageView().getAnimation();
        if (animation != null) {
            animation.cancel();
        }
    }
}